package com.jmp.api.controller;

import com.jmp.base.AccountApi;
import com.jmp.base.dto.*;
import lombok.extern.slf4j.Slf4j;
import one.stand.model.CompanyAccount;
import one.stand.model.ResultModel;
import com.jmp.api.service.CompanyAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@Slf4j
public class AccountController implements AccountApi {
    @Autowired
    private CompanyAccountService companyAccountService;

    @Override
    public ResultModel save(@RequestBody AccountSaveRequest request) {
        companyAccountService.save(request.getCompanyId(), request.getAccountType(), request.getAppid(), request.getAppSecret(), request.getAppName());
        return ResultModel.success();
    }

    @Override
    public ResultModel<AccountResponse> auth(@RequestBody AccountAuthRequest request) {
        CompanyAccount companyAccount = companyAccountService.auth(request.getAppId());
        return ResultModel.success(get(companyAccount));
    }

    @Override
    public ResultModel<AccountResponse> getCompanyIdDefaultIfNull(@RequestBody AccountCompanyIdRequest request) {
        CompanyAccount companyAccountModel = companyAccountService.getDefaultIfNull(request.getCompanyId(), request.getLoginEnum());
        return ResultModel.success(get(companyAccountModel));
    }

    @Override
    public ResultModel<AccountResponse> getCompanyId(@RequestBody AccountCompanyIdRequest request) {
        CompanyAccount companyAccountModel = companyAccountService.get(request.getCompanyId(), request.getLoginEnum());
        return ResultModel.success(get(companyAccountModel));
    }

    @Override
    public ResultModel<AccountResponse> getCompanyKeyDefaultIfNull(@RequestBody AccountCompanyKeyRequest request) {
        CompanyAccount companyAccountModel = companyAccountService.getDefaultIfNull(request.getCompanyKey(), request.getLoginEnum());
        return ResultModel.success(get(companyAccountModel));
    }

    @Override
    public ResultModel<AccountResponse> getByAppId(@RequestBody AccountAppIdRequest request) {
        CompanyAccount companyAccountModel = companyAccountService.getByAppId(request.getAppId());
        return ResultModel.success(get(companyAccountModel));
    }

    @Override
    public ResultModel<AccountResponse> getByAppId2(@RequestBody AccountAppIdRequest request) {
        CompanyAccount companyAccountModel = companyAccountService.getByAppId2(request.getAppId());
        return ResultModel.success(get(companyAccountModel));
    }

    private AccountResponse get(CompanyAccount account) {
        if (Objects.isNull(account)) return null;

        return AccountResponse.builder()
                .accountId(account.getAccountId())
                .companyId(account.getCompanyId())
                .companyKey(account.getCompanyKey())
                .accountType(account.getAccountType())
                .appName(account.getAppName())
                .wechatId(account.getWechatId())
                .appId(account.getAppId())
                .appSecret(account.getAppSecret())
                .key(account.getKey())
                .merchantId(account.getMerchantId())
                .certData(account.getCertData())
                .redirectUri(account.getRedirectUri())
                .build();
    }
}
